// 全新的路由配置文件
import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/HomeView.vue';
import LoginView from '../views/LoginView.vue';
import RegisterView from '../views/RegisterView.vue';
import CharacterDetailView from '../views/CharacterDetailView.vue';
import ChatView from '../views/ChatView.vue';
import { authStore } from '../store/auth';

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView
  },
  {
    path: '/characters/:id',
    name: 'character-detail',
    component: CharacterDetailView
  },
  {
    path: '/chat/:characterId',
    name: 'chat',
    component: ChatView,
    meta: {
      requiresAuth: true
    }
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL || '/'),
  routes
});

// 路由守卫
router.beforeEach((to, from, next) => {
  const auth = authStore();
  
  if (to.meta.requiresAuth && !auth.isAuthenticated) {
    next({ name: 'login' });
  } else {
    next();
  }
});

export default router;